﻿using System;
using System.Collections.Generic;
using HiTeachers.Entities;
using HiTeachers.Services;

namespace HiTeachers.Web
{
    public partial class AdjustPrice : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) return;

            Page.CloseMessage(lblMsg);

            BindingPeriodDropdown();
            BindingPrice();
        }

        protected void BtnUpdateClick(object sender, EventArgs e)
        {
            decimal newFee = Convert.ToDecimal(txtFees.Text);
            FeeConfig feeConfig = FeeConfigServices.GetById(ddlPeriod.SelectedValue);
            feeConfig.Fees = newFee;

            FeeConfigServices.SaveOrUpdate(feeConfig);
            txtFees.Text = newFee.ToString("#,#.00#");
            Page.DisplayMessage(lblMsg, "Cập nhật thành công!", MessageType.Success);
        }

        protected void DdlPeriodSelectedIndexChanged(object sender, EventArgs e)
        {
            BindingPrice();
        }

        #region Private Methods
        private void BindingPeriodDropdown()
        {
            var lstFees = new List<FeeConfig>(FeeConfigServices.GetAll());
            lstFees.Sort((it1, it2) => it1.Period.CompareTo(it2.Period));

            ddlPeriod.DataValueField = "ID";
            ddlPeriod.DataTextField = "Name";
            ddlPeriod.DataSource = lstFees;//FeeConfigServices.GetAll();
            ddlPeriod.DataBind();
        }

        private void BindingPrice()
        {
            FeeConfig feeConfig = FeeConfigServices.GetById(ddlPeriod.SelectedValue);
            txtFees.Text = feeConfig.Fees.ToString("#,#.00#");
        }
        #endregion
    }
}
